package cn.kgc.dao.impl;

import cn.kgc.dao.AccountDao;
import cn.kgc.entity.Account;
import cn.kgc.utils.ConnectionUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Rock
 * @create 2020-08-25 9:49
 */
@Repository("AccountDao")
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private QueryRunner queryRunner;
    @Autowired
    private ConnectionUtil connectionUtil;
    /**
     * 根据账号查询账户
     *
     * @param accountNum
     */
    public Account getAccountByNum(String accountNum) throws SQLException {
        List<Account> accountList=queryRunner.query(connectionUtil.getConnectin(),"select * from account where accountNum=?",new BeanListHandler<Account>(Account.class),accountNum);
        if(accountList.size()==0){
            return null;
        }else{
            return accountList.get(0);
        }
    }

    /**
     * 更新账户
     *
     * @param account
     */
    public Integer updateAccount(Account account) throws SQLException {
        return queryRunner.update(connectionUtil.getConnectin(),"update account set accountMoney=? where accountNum=?",account.getAccountMoney(),account.getAccountNum());
    }
}
